--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local config = require "configquery"


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch, node_message)

	self.message_dispatch = message_dispatch
	self.node_message = node_message

    self.user_game = {} --user所在game

	self:register()
end

--注册本服务里的消息
function MessageHandler:register()
	--启动
	self.message_dispatch:registerSelf('start', handler(self,self.start))	
    self.message_dispatch:register(handler(self,self.dispatchToGame)) --未注册的消息会透传到对应的游戏中

    self.message_dispatch:registerSelf('enterReq', handler(self, self.enterReq))
    self.message_dispatch:registerSelf('remove_user', handler(self, self.removeUser))
    -- self.message_dispatch:registerSelf("disconnect", handler(self,self.disconnect))
end






------------------------------------------------------------------
--cmd
------------------------------------------------------------------
function MessageHandler:start()
	-- local pbc_env = self.proto_loader:init(self.proto_file, {'common','game','gate','web','zone','hall',})
    self.server_manager = global.server_manager
    self.game_config = global.game_config
    self.game_manager = global.game_manager

	--初始化
	-- self.node_message:initProto(pbc_env)
	--skynet控制台
	-- skynet.newservice("debug_console", svr_config.debug_port)

end


--派发消息到游戏桌子中
function MessageHandler:dispatchToGame(cmd, ...)
    print("_____gameserver__收到dispatch___", cmd, ...)
    local data = ...
    local user_id = data.user_id
    local game = self.game_manager:getGameByUserId(user_id) 
    if game then 
        --传到游戏服务中
        local res = self.node_message:callService(game:getService(), cmd, ...)
        if res and next(res) then 
            return res
        end
    end
    --未找到游戏服务处理消息
    error_log("___未找到游戏服务处理消息__")
    return false
end

--玩家进入游戏
function MessageHandler:enterReq(data)
    print("__________enterReq___", data)
    local game_id = data.game_id
    local table_id = data.table_id
    local user_id = data.user_id
    local game = self.game_manager:getGame(table_id)
    if not game then 
        game = self.game_manager:createGame(table_id)
        game:start()
    end
    local table_service = game:getService()
    print("________table_service____",table_service)
    local res =  self.node_message:callService(table_service, "enter", data)

    if res and next(res) then
        --进入桌子成功
        self.game_manager:setUserGame(user_id, game)
    end

    return res
end

--玩家从游戏中离开
function MessageHandler:removeUser(user_id)
    self.game_manager:removeUser(user_id)
end

-- --用户掉线
-- function MessageHandler:disconnect(user_id)
--     debug_log("_________hall_disconnect__",user_id)
--     self.user_manager:removeUser(user_id)
-- end


return MessageHandler